home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / update-notifier / apt_check.py next >
Encoding:
Python Source  |  2009-03-27  |  5.3 KB  |  163 lines

  1. #!/usr/bin/python
  2.  
  3.  
  4. #nice apt-get -s -o Debug::NoLocking=true upgrade | grep ^Inst 
  5.  
  6. import apt_pkg
  7. import os
  8. import sys
  9. from optparse import OptionParser
  10. import gettext
  11.  
  12. SYNAPTIC_PINFILE = "/var/lib/synaptic/preferences"
  13.  
  14. def _(msg):
  15.     return gettext.dgettext("update-notifier", msg)
  16.  
  17. class OpNullProgress(object):
  18.     def update(self, percent):
  19.         pass
  20.     def done(self):
  21.         pass
  22.  
  23. def clean(cache,depcache):
  24.     # mvo: looping is too inefficient with the new auto-mark code
  25.     #for pkg in cache.Packages:
  26.     #    depcache.MarkKeep(pkg)
  27.     depcache.Init()
  28.  
  29. def saveDistUpgrade(cache,depcache):
  30.     """ this functions mimics a upgrade but will never remove anything """
  31.     depcache.Upgrade(True)
  32.     if depcache.DelCount > 0:
  33.         clean(cache,depcache)
  34.     depcache.Upgrade()
  35.  
  36. def _handleException(type, value, tb):
  37.     sys.stderr.write("E: "+ _("Unkown Error: '%s' (%s)") % (type,value))
  38.     sys.exit(-1)
  39.  
  40.  
  41. def isSecurityUpgrade(ver):
  42.     " check if the given version is a security update (or masks one) "
  43.     for (file, index) in ver.FileList:
  44.         if (file.Archive.endswith("-security") and
  45.             file.Origin == "Ubuntu"):
  46.             return True
  47.     return False
  48.     
  49. def run(options=None):
  50.     # be nice
  51.     os.nice(19)
  52.     # FIXME: do a ionice here too?
  53.     
  54.     # init
  55.     apt_pkg.init()
  56.  
  57.     # force apt to build its caches in memory for now to make sure
  58.     # that there is no race when the pkgcache file gets re-generated
  59.     apt_pkg.Config.Set("Dir::Cache::pkgcache","")
  60.  
  61.     # get caches
  62.     try:
  63.         cache = apt_pkg.GetCache(OpNullProgress())
  64.     except SystemError, e:
  65.         sys.stderr.write("E: "+ _("Error: Opening the cache (%s)") % e)
  66.         sys.exit(-1)
  67.     depcache = apt_pkg.GetDepCache(cache)
  68.  
  69.     # read the pin files
  70.     depcache.ReadPinFile()
  71.     # read the synaptic pins too
  72.     if os.path.exists(SYNAPTIC_PINFILE):
  73.         depcache.ReadPinFile(SYNAPTIC_PINFILE)
  74.  
  75.     # init the depcache
  76.     depcache.Init()
  77.  
  78.     if depcache.BrokenCount > 0:
  79.         sys.stderr.write("E: "+ _("Error: BrokenCount > 0"))
  80.         sys.exit(-1)
  81.  
  82. # do the upgrade (not dist-upgrade!)
  83.     try:
  84.         saveDistUpgrade(cache,depcache)
  85.     except SystemError, e:
  86.         sys.stderr.write("E: "+ _("Error: Marking the upgrade (%s)") % e)
  87.         sys.exit(-1)
  88.  
  89.     # check for upgrade packages, we need to do it this way
  90.     # because of ubuntu #7907
  91.     upgrades = 0
  92.     security_updates = 0
  93.     for pkg in cache.Packages:
  94.         if depcache.MarkedInstall(pkg) or depcache.MarkedUpgrade(pkg):
  95.             inst_ver = pkg.CurrentVer
  96.             cand_ver = depcache.GetCandidateVer(pkg)
  97.             # check if this is really a upgrade or a false positive
  98.             # (workaround for ubuntu #7907)
  99.         if cand_ver != inst_ver:
  100.                 # check for security upgrades
  101.                 upgrades = upgrades + 1    
  102.                 if isSecurityUpgrade(cand_ver):
  103.                     security_updates += 1
  104.                 # now check for security updates that are masked by a 
  105.                 # canidate version from another repo (-proposed or -updates)
  106.                 for ver in pkg.VersionList:
  107.                     if (inst_ver and apt_pkg.VersionCompare(ver.VerStr, inst_ver.VerStr) <= 0):
  108.                         #print "skipping '%s' " % ver.VerStr
  109.                         continue
  110.                     if isSecurityUpgrade(ver):
  111.                         security_updates += 1
  112.                         break
  113.  
  114.     # print the number of upgrades
  115.     if options and options.show_package_names:
  116.         pkgs = filter(lambda pkg: depcache.MarkedInstall(pkg) or depcache.MarkedUpgrade(pkg), cache.Packages)
  117.         sys.stderr.write("\n".join(map(lambda p: p.Name, pkgs)))
  118.     elif options and options.readable_output:
  119.         print gettext.dngettext("update-notifier",
  120.                                 "%i package can be updated.",
  121.                                 "%i packages can be updated.",
  122.                                 upgrades) % upgrades
  123.         print gettext.dngettext("update-notifier",
  124.                                 "%i update is a security update.",
  125.                                 "%i updates are security updates.",
  126.                                 security_updates)  % security_updates
  127.     else:
  128.         # print the number of regular upgrades and the number of 
  129.         # security upgrades
  130.         sys.stderr.write("%s;%s" % (upgrades,security_updates))
  131.  
  132.     # return the number of upgrades (if its used as a module)
  133.     return(upgrades,security_updates)
  134.  
  135.  
  136. if __name__ == "__main__":        
  137.     # setup a exception handler to make sure that uncaught stuff goes
  138.     # to the notifier
  139.     sys.excepthook = _handleException
  140.     
  141.     # gettext
  142.     APP="update-notifier"
  143.     DIR="/usr/share/locale"
  144.     gettext.bindtextdomain(APP, DIR)
  145.     gettext.textdomain(APP)
  146.  
  147.     # check arguments
  148.     parser = OptionParser()
  149.     parser.add_option("-p",
  150.                       "--package-names",
  151.                       action="store_true",
  152.                       dest="show_package_names",
  153.                       help=_("Show the packages that are going to be installed/upgraded"))
  154.     parser.add_option("--human-readable",
  155.                       "--human-readable",
  156.                       action="store_true",
  157.                       dest="readable_output",
  158.                       help=_("Show human readable output on stdout"))
  159.     (options, args) = parser.parse_args()
  160.  
  161.     # run it
  162.     run(options)
  163.